package com.vtest.quartz.service.impl.wip;

import com.jcraft.jsch.*;
import com.vtest.quartz.service.vtest.wip.WipReportService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2025-01-16 14:56
 * @Description :WIP 报表 service层
 */

@Service
public class WipReportServiceImpl implements WipReportService {

    /**
     * @Description: 获取WIP报表最新的数据
     * code : 客户代码
     * prodCat : CP/FT
     */
    public  String getLatestFileName(String FilePath, String code, String prodCat) {

        StringBuilder sb = new StringBuilder(code);
        sb.append("_");

        if (prodCat.equals("CP")) sb.append("WipAndInventoryReport");
        if (prodCat.equals("FT")) sb.append("WipAndInventoryReport");

        String finalFileName = "";
        File excelPath = new File(FilePath);
        if (excelPath.exists()) {
            File[] excels = excelPath.listFiles();
            for (File excel : excels) {
                String filename = excel.getName();
                if (filename.contains(sb.toString()) && filename.compareTo(finalFileName) > 0) {
                    finalFileName = filename;
                }
            }
        }{
            System.out.println("路径不存在"+FilePath);
        }

        return finalFileName;
    }

    public static void main(String[] args) {
        //WipReportServiceImpl wipReportService = new WipReportServiceImpl();
        //wipReportService.getLatestFileName("\\\\192.168.10.216\\Reports"
              //  ,"HCP","CP");

        String user = "root"; // 服务器用户名
        String password = "admin123#"; // 服务器密码
        String host = "192.168.10.212"; // 服务器地址
        int port = 22; // SSH端口，默认是22


        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            String command = "ls -l"; // 你想执行的命令，例如列出文件
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            java.io.InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: " + channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }
            channel.disconnect();
            session.disconnect();
        } catch (JSchException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
